package com.mc.lab.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.mc.common.constant.Constants;
import com.mc.clt.domain.CltResult;
import com.mc.clt.mapper.CltResultMapper;
import com.mc.clt.service.impl.CltServiceImpl;
import com.mc.common.utils.DateUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.mc.lab.mapper.LabAuthenticateMapper;
import com.mc.lab.domain.LabAuthenticate;
import com.mc.lab.service.ILabAuthenticateService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 鉴定 Service业务层处理
 *
 * @author mc
 * @date 2021-03-18
 */
@Service
public class LabAuthenticateServiceImpl implements ILabAuthenticateService
{
    @Autowired
    private LabAuthenticateMapper labAuthenticateMapper;
    @Autowired
    private CltResultMapper cltResultMapper;
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(LabAuthenticateServiceImpl.class);


    /**
     * 查询鉴定
     *
     * @param authenticateId 鉴定 ID
     * @return 鉴定
     */
    @Override
    public LabAuthenticate selectLabAuthenticateById(String authenticateId)
    {
        return labAuthenticateMapper.selectLabAuthenticateById(authenticateId);
    }

    /**
     * 查询鉴定 列表
     *
     * @param labAuthenticate 鉴定
     * @return 鉴定
     */
    @Override
    public List<LabAuthenticate> selectLabAuthenticateList(LabAuthenticate labAuthenticate)
    {
        return labAuthenticateMapper.selectLabAuthenticateList(labAuthenticate);
    }

    /**
     * 新增鉴定
     *
     * @param labAuthenticate 鉴定
     * @return 结果
     */
    @Transactional//多次插入开启事务
    @Override
    public int insertLabAuthenticate(LabAuthenticate labAuthenticate)
    {
        labAuthenticate.setCreateTime(DateUtils.getNowDate());


        CltResult cltResult = cltResultMapper.selectCltResultById(labAuthenticate.getCltId());
        if(cltResult.getIsAuthenticate().equals("0")){
            cltResult.setIsAuthenticate("1");
            cltResult.setType("1");
            cltResult.setUpdateUserId(labAuthenticate.getCreateUserId());
            cltResult.setUpdateTime(DateUtils.getNowDate());
            cltResult.setResultId(labAuthenticate.getCltId());
            cltResultMapper.updateCltResult(cltResult);
        }
        cltResult = cltResultMapper.selectCltResultById(labAuthenticate.getCltId());
        try{
            restTemplate(cltResult,labAuthenticate);
        }catch (Exception e){

        }
        return labAuthenticateMapper.insertLabAuthenticate(labAuthenticate);
    }

    /**
     * 修改鉴定
     *
     * @param labAuthenticate 鉴定
     * @return 结果
     */
    @Override
    public int updateLabAuthenticate(LabAuthenticate labAuthenticate)
    {
        labAuthenticate.setUpdateTime(DateUtils.getNowDate());
        return labAuthenticateMapper.updateLabAuthenticate(labAuthenticate);
    }

    /**
     * 批量删除鉴定
     *
     * @param authenticateIds 需要删除的鉴定 ID
     * @return 结果
     */
    @Override
    public int deleteLabAuthenticateByIds(String[] authenticateIds)
    {
        return labAuthenticateMapper.deleteLabAuthenticateByIds(authenticateIds);
    }

    /**
     * 删除鉴定 信息
     *
     * @param authenticateId 鉴定 ID
     * @return 结果
     */
    @Override
    public int deleteLabAuthenticateById(String authenticateId)
    {
        return labAuthenticateMapper.deleteLabAuthenticateById(authenticateId);
    }

    @Override
    public List<LabAuthenticate> selectLabAuthenticateListByCltID(String cltId) {
        return labAuthenticateMapper.selectLabAuthenticateListByCltID(cltId);
    }

    @Override
    public LabAuthenticate selectLabAuthenticateByCltID(String cltId) {
        return labAuthenticateMapper.selectLabAuthenticateByCltID(cltId);
    }

    public String restTemplate(CltResult clt,LabAuthenticate lab){
        RestTemplate restTemplate = new RestTemplate();
        log.info("================ start =====================");
        MultiValueMap<String, String> params= new LinkedMultiValueMap<String, String>();
        params.add("f","pjson");
        params.add("frollbackOnFailure", "true");
        params.add("features", "[{\"attributes\": {\n" +
                "    \"OBJECTID\":"+clt.getObjectId()+"\n" +
                ",\"NAME\": \""+lab.getName()+"\",\n" +
                "\"FAMILY\":\""+lab.getFamilyName()+"\",\n" +
                "\"GENUS\":\""+lab.getCategoryName()+"\",\n" +
                "\"SPECIES\":\""+lab.getScientificName()+"\"\n" +
                "   }\n" +
                "}]");
        log.info("params:"+params.toString());
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, null);
        ResponseEntity<String> response = restTemplate.postForEntity(
                Constants.GIS_SERVER_URL+"updateFeatures",
                requestEntity,
                String .class);
        String jsonObject = response.getBody();
        return JSONObject.parseObject(jsonObject).getJSONArray("updateResults").getJSONObject(0).getString("objectId");

    }
}